package com.sskj.login.util;

import android.content.Context;
import android.support.transition.ChangeClipBounds;
import android.support.transition.Scene;
import android.support.transition.TransitionManager;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.TextInput;
import com.sskj.common.base.App;
import com.sskj.lib.util.SchemeUtil;
import com.sskj.login.R;

public class AreaCodeUtil {
    private LinearLayout llTab;
    private Scene mSceneLeft;
    private Scene mSceneRight;
    private LinearLayout llAreaCode;
    private EditText etAccount;
    private TextInput<EditText> editTextAccount;
    private AndroidNextInputs[] inputs;
    private String leftTitle, rightTitle;

    public void init(Context context, String leftTitle, String rightTitle, LinearLayout llTab, LinearLayout llAreaCode, EditText etAccount, TextInput<EditText> editTextAccount, AndroidNextInputs... inputs) {
        this.llTab = llTab;
        this.llAreaCode = llAreaCode;
        this.etAccount = etAccount;
        this.editTextAccount = editTextAccount;
        this.inputs = inputs;
        this.leftTitle = leftTitle;
        this.rightTitle = rightTitle;
        ((TextView) (llTab.findViewById(R.id.tvPhone))).setText(leftTitle);
        ((TextView) (llTab.findViewById(R.id.tvEmail))).setText(rightTitle);
        mSceneLeft = Scene.getSceneForLayout(llTab, R.layout.login_include_tab_left, context);
        mSceneRight = Scene.getSceneForLayout(llTab, R.layout.login_include_tab_right, context);
        initEmail();
    }

    private void initPhone() {
        ((TextView) (llTab.findViewById(R.id.tvPhone))).setText(leftTitle);
        ((TextView) (llTab.findViewById(R.id.tvEmail))).setText(rightTitle);
        llTab.findViewById(R.id.tvPhone).setOnClickListener(v -> {

            TransitionManager.go(mSceneLeft, new ChangeClipBounds().setDuration(200));
            initEmail();
            etAccount.setText("");
            llAreaCode.setVisibility(View.VISIBLE);
            etAccount.setHint(App.INSTANCE.getString(R.string.loginlogin_include_account40));
            etAccount.setInputType(InputType.TYPE_CLASS_PHONE);
            for (AndroidNextInputs input : inputs) {
                input.remove(editTextAccount).add(editTextAccount, SchemeUtil.notEmpty(etAccount), SchemeUtil.phone());
            }
        });
    }

    private void initEmail() {
        ((TextView) (llTab.findViewById(R.id.tvPhone))).setText(leftTitle);
        ((TextView) (llTab.findViewById(R.id.tvEmail))).setText(rightTitle);
        llTab.findViewById(R.id.tvEmail).setOnClickListener(v -> {

            TransitionManager.go(mSceneRight, new ChangeClipBounds().setDuration(200));
            initPhone();
            etAccount.setText("");
            etAccount.setHint(App.INSTANCE.getString(R.string.login_areaCodeUtil2));
            llAreaCode.setVisibility(View.GONE);
            etAccount.setInputType(InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS);
            for (AndroidNextInputs input : inputs) {
                input.remove(editTextAccount).add(editTextAccount, SchemeUtil.notEmpty(etAccount), SchemeUtil.email());
            }
        });
    }
}
